const service = 'wx2d1fd8562c42cebb'
const HOST = 'https://devbbq.cn'
export function detectface(picUrl) {
  return new Promise((resolve, reject) => {
      wx.serviceMarket.invokeService({
        service,
        api: 'detectFace',
        data: {
          "Action": "DetectFace",
          "Url": picUrl,
          "NeedFaceAttributes": 1,
          },
        
      }).then(res => {
        const { data } = res
        resolve(data)
        // console.log('invokeService success', FaceInfos, ImageHeight, ImageWidth)
      }).catch(err => {
        reject(err)
        console.error('invokeService fail', err)
      })
  })
}

export function segmentPortraitPic(picUrl) {
  return new Promise((resolve, reject) => {
      wx.serviceMarket.invokeService({
        service,
        api: 'segmentPortraitPic',
        data: {
          "Action": "SegmentPortraitPic",
          // "Image": canvas.toDataURL()
          "Url": picUrl
        },
      }).then(async(res) => {
        console.log(res, 89);
        const { data:{ ResultImageUrl }} = res
        // writePic(ResultImageUrl)
        // 开始鉴黄
       const errcode = await mediaCheckImg(ResultImageUrl)
       if(errcode === 87014) {
          reject(errcode)
       } else {
         resolve(ResultImageUrl)
       }
      }).catch(err => {
        reject(err)
        console.error('出错啦', err)
      })
  })
}

function writePic(picUrl) {
  wx.request({
    method: "POST",
    url: `${HOST}/userPic`,
    data: {
      picUrl
    },
    header: {
          'content-type': 'application/json' // 默认值
        },
        success: (res) => {
        }
  })
}
// 图片鉴黄
export function mediaCheckImg(imgUrl) {
  return new Promise((resolve, reject) => {
    console.log("开始鉴黄", imgUrl);
    wx.request({
      header: {
        'content-type': 'application/json' // 默认值
      },
      method: "POST",
      url: `${HOST}/mediaCheck`,
      data: {
        imgUrl
      },
      success: (res) => {
        const {
          data: {
            errcode
          }
        } = res
        resolve(errcode)
      }
    })
  })
}